from typing import List, Optional
from fastapi import APIRouter, HTTPException

from app.customer.models import Customer
from app.customer.schemas import CustomerSchema, CustomerCreate, CustomerUpdate, CustomerRelated
from app.customer.crud import customer

customer_router = APIRouter()

@customer_router.get('/{customer_id}', response_model=Optional[CustomerRelated])
async def get_customer(customer_id: int) -> Optional[Customer]:
    get_customer = await customer.get(id=customer_id)
    if not get_customer:
        raise HTTPException(
            status_code=404,
            detail="The customer with this id does not exist in the system",
        )
    return get_customer

@customer_router.get('/', response_model=List[CustomerSchema])
async def get_customers() -> List[Customer]:
    get_customers = await customer.get_multy()
    return get_customers

@customer_router.post('/create', response_model=CustomerSchema)
async def create_customer(customer_schema: CustomerCreate) -> Optional[Customer]:
    new_customer = await customer.create(schema=customer_schema)
    return new_customer

@customer_router.put('/update/{customer_id}', response_model=CustomerSchema)
async def update_customer(customer_id: int, customer_schema: CustomerUpdate) -> Optional[Customer]:
    updating_customer = await customer.update(id=customer_id, schema=customer_schema)
    if not updating_customer:
        raise HTTPException(
            status_code=404,
            detail="The customer with this id does not exist in the system",
        )
    return updating_customer

@customer_router.delete('/delete/{customer_id}', response_model=Customer)
async def delete_customer(customer_id: int) -> Optional[Customer]:
    deleting_customer = await customer.delete(id=customer_id)
    if not deleting_customer:
        raise HTTPException(
            status_code=404,
            detail="The customer with this id does not exist in the system",
        )
    return deleting_customer
